package com.mall.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.api.PmsProductCategoryApi;
import com.mall.pojo.PmsProductCategory;
import com.mall.vo.PmsProductCategoryVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PmsProductCategoryService {

    @DubboReference
    private PmsProductCategoryApi pmsProductCategoryApi;

    //添加商品分类
    public void create(PmsProductCategory pmsProductCategory) {
        pmsProductCategoryApi.create(pmsProductCategory);
    }

    //删除商品分类
    public void delete(Integer id) {
        pmsProductCategoryApi.delete(id);
    }

    //分页获取所有商品属性分类
    public Page<PmsProductCategory> list(Integer parentId, Integer pageNum, Integer pageSize) {
        return pmsProductCategoryApi.list(parentId, pageNum, pageSize);
    }

    //修改导航栏显示状态
    public void navStatus(List<Integer> ids, Integer navStatus) {
        pmsProductCategoryApi.navStatus(ids, navStatus);
    }

    //修改显示状态
    public void showStatus(List<Integer> ids, Integer showStatus) {
        pmsProductCategoryApi.showStatus(ids, showStatus);
    }

    //修改商品分类
    public void update(List<Integer> id, PmsProductCategory pmsProductCategory) {
        pmsProductCategoryApi.update(id, pmsProductCategory);
    }

    //根据id获取商品分类
    public PmsProductCategory selectById(Integer id) {
        return pmsProductCategoryApi.selectById(id);
    }

    //查询所有一级分类及子分类
    public List<PmsProductCategoryVo> withChildren() {
        List<PmsProductCategory> pmsProductCategoryList = pmsProductCategoryApi.selectAll();//查询一级分类
        List<PmsProductCategoryVo> list = pmsProductCategoryList.stream().map(pmsProductCategory -> {
            PmsProductCategoryVo vo = new PmsProductCategoryVo();
            BeanUtil.copyProperties(pmsProductCategory, vo);
            //根据一级分类id查询子分类
            List<PmsProductCategory> categoryList = pmsProductCategoryApi.selectByParentId(pmsProductCategory.getId());
            vo.setChildren(categoryList);
            return vo;
        }).collect(Collectors.toList());
        return list;
    }
}
